/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TaBox.client;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author timo
 */
public class FileChecker {
 
    String path = "D://TaBox";
    String[] lastsync;
    String[] syncdel;
    ArrayList<String> del;
    
    
    public FileChecker(){      
        del = new ArrayList<>();
        syncdel = new String[1];
        syncdel[0] = "SYNCDEL";
    }
    
    File[] getFolderList(File[] e){
              
        ArrayList<File> al = new ArrayList<File>();
        
        for (int t = 0; t < e.length; t++){
            if (e[t].isDirectory()){
                al.add(e[t]);
            }    
        }
        File[] hasil = new File[al.size()];
        al.toArray(hasil);
        return hasil;
    }
    
    File[] getFileList(File[] e){
              
        ArrayList<File> al = new ArrayList<File>();
        
        for (int t = 0; t < e.length; t++){
            if (e[t].isFile()){
                al.add(e[t]);
            }    
        }
        File[] hasil = new File[al.size()];
        al.toArray(hasil);
        return hasil;
    }
    
    String getPath(File f){
        
        String p = f.getAbsolutePath();
        String hasil = p.substring(p.indexOf("TaBox") + 6);
        return hasil;
        
    }
    
   String getMessage(File f){
       
       return (getPath(f) + " " + String.valueOf(f.lastModified()));
       
   }
    
   public void queueMessage(OutputStream out,File[] file){
              
       File[] listfolder = getFolderList(file);
       File[] listfile = getFileList(file);
      
       for (int y = 0; y < listfolder.length; y++){
           del.add(getPath(listfolder[y]));
            try {
                out.write(getMessage(listfolder[y]).getBytes());
            } catch (IOException ex) {
                Logger.getLogger(FileChecker.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
       
       for (int y = 0; y < listfile.length; y++){
           del.add(getPath(listfile[y]));
            try {
                out.write(getMessage(listfile[y]).getBytes());
            } catch (IOException ex) {
                Logger.getLogger(FileChecker.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
       
       for (int y = 0; y < listfolder.length; y++){
           sendMessage(out,listfolder[y].listFiles());         
       }
       
   }
   
   public void writeFile(String path,byte[] b){
       
       File file = new File(path);
        try {
            file.createNewFile();       
            try (FileOutputStream f = new FileOutputStream(file)) {
                f.write(b);
            }            
        } catch (IOException ex) {
            Logger.getLogger(FileChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
                  
   }
   
   public void checkDel(){
       
       
       
       
   }
   
   
   
}
